﻿@model IEnumerable<HomeFloor>
@{

    ViewBag.Title = "楼层管理";
}

<style>
    /*树表格样式*/
    .table s.line {
        margin: 0 6px;
        display: inline-block;
        text-decoration: none;
        color: #ccc;
    }

    .table s.line-lv3 {
        margin-left: 55px;
        margin-right: 20px;
    }

    .btn-a a, .btn-a .good-share {
        cursor: pointer;
        transition: all linear .2s;
        padding: 0 8px;
    }

        .btn-a a + a {
            border-left: 1px solid #e6e6e6;
        }
</style>

<div class="row">
    <div class="col-md-12">

        <div class="portlet light">
            <div class="section-header sticky">
                <div class="caption">
                    <i class="icon-equalizer font-red-sunglo"></i>
                    <span class="caption-subject font-red-sunglo bold uppercase">楼层编辑</span>
                    <span class="caption-helper">@Html.ActionLink("(" + T("Admin.Common.BackToList") + ")", "Index")</span>
                </div>
                <div class="tools">

                </div>
                <div class="actions">
                    <a href="@Url.Action("AddHomeFloorDetail")" class="btn yellow"><i class="fa fa-plus"></i>楼层一 </a>
                    <a href="@Url.Action("AddHomeFloorDetail2")" class="btn blue"><i class="fa fa-plus"></i>楼层二 </a>
                    <a href="@Url.Action("AddHomeFloorDetail3")" class="btn yellow"><i class="fa fa-plus"></i>楼层三 </a>
                </div>
            </div>

            <div class="tableContainer portlet-body flip-scroll">
                <table class="table table-striped table-bordered table-hover dataTable no-footer category_table">
                    <thead>
                        <tr>
                            <th>楼层名称</th>
                            <th>楼层类型</th>
                            <th>排序</th>
                            <th></th>
                            <th class="td-operate">操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach (var floor in Model)
            {
                            <tr rownumber="@floor.DisplayOrder" floorId="@floor.Id">
                                <td>@floor.Name</td>
                                <td>
                                    @if (floor.StyleLevel == 0)
                                    { <text>楼层一</text>}
                                    else if (floor.StyleLevel == 1)
                                    {<text>楼层二</text>}
                                    else
                                    {<text>楼层三</text>}
                                </td>
                                <td width="100px"><span class="glyphicon glyphicon-circle-arrow-up"></span> <span class="glyphicon glyphicon-circle-arrow-down"></span></td>
                                <td width="120px" align="center"><div class="switch switch-mini"><input class="bootstrapSwitch" type="checkbox" data-size="mini" data-on-text="开启" data-off-text="关闭" @(floor.Enable ? "checked" : "") /></div></td>
                                <td class="td-operate">
                                    <span class="btn-a">
                                        @if (floor.StyleLevel == 0)
                                        {
                                            <a href="AddHomeFloorDetail/@floor.Id">编辑</a>
                                        }
                                        else if (floor.StyleLevel == 1)
                                        {
                                            <a href="AddHomeFloorDetail2/@floor.Id">编辑</a>
                                        }
                                        else
                                        {
                                            <a href="AddHomeFloorDetail3/@floor.Id">编辑</a>
                                        }
                                        <a onclick="deleteFloor(@floor.Id,'@floor.Name')">删除</a>
                                    </span>
                                </td>
                            </tr>
                        }
                    </tbody>
                </table>
            </div>
        </div>

    </div>
</div>

<script type="text/javascript">
    function changeSequence(oriRowNumber, newRowNumber, callback) {
        var result = false;
        $.ajax({
            type: 'post',
            url: 'FloorChangeSequence',
            cache: false,
            async: true,
            data: { oriRowNumber: oriRowNumber, newRowNumber: newRowNumber },
            dataType: "json",
            success: function (data) {
                if (!data.Result)
                    toastr['error']('调整顺序出错.' + data.msg, '');
                else
                    callback();
            },
            error: function () {
            }
        });
    }


    function changSwitch(id, enable, callback) {

        var result = false;
        $.ajax({
            type: 'post',
            url: 'FloorEnableDisplay/'+id,
            cache: false,
            async: true,
            data: {  enable: enable },
            dataType: "json",
            success: function (data) {

                if (data.Result)
                    callback(data.Result);
            },
            error: function () {

            }
        });
    }


    function deleteFloor(id, name) {

        bootbox.confirm('确定要删除楼层 ' + name + ' 吗？', function (result) {
            if (result) {
                $.post('DeleteFloor/' + id, function (result) {
                    if (result.Result) {
                        toastr['success']('删除成功.', '');
                        $('tr[floorId="' + id + '"]').fadeOut(1000, function () { $(this).remove(); });
                    }
                    else
                        toastr['success']('删除失败.' + result.Message, '');
                });
            }
        });

    }



    $(function () {

        $(".bootstrapSwitch").bootstrapSwitch({
            onSwitchChange: function (event, state) {
                var id = $(this).parents('tr').attr('floorId');
                var switcher = $(this);
                changSwitch(id, state, function (result) {
                    if (result != undefined && result) {
                        switcher.bootstrapSwitch('state', state);
                    }
                    else {
                        switcher.bootstrapSwitch('state', !state);
                        toastr['success']('删除失败.' + result.Message, '');
                    }
                });
            }
        });

        //开关
        $(".bootstrapSwitch").bootstrapSwitch();

        //排序
        $(".table tbody tr").first().find('.glyphicon-circle-arrow-up').addClass('disabled');
        $(".table tbody tr").last().find('.glyphicon-circle-arrow-down').addClass('disabled');
        $(".table").on("click", '.glyphicon-circle-arrow-up', function () {
            var thisObj = this;
            var p = $(this).parents('tr');
            var index = p.parent().find('tr').index(p);
            if (index == 0)
                return false;
            else {
                var oriRowNumber = parseInt(p.attr('rowNumber'));
                var newRowNumber = p.prev().attr('rowNumber');
                changeSequence(oriRowNumber, newRowNumber, function () {
                    p.attr('rowNumber', newRowNumber);
                    p.prev().attr('rowNumber', oriRowNumber);
                    p.prev().before(p);
                    reDrawArrow(thisObj);
                });
            }
        });
        $(".table").on("click", '.glyphicon-circle-arrow-down', function () {
            var thisObj = this;
            var p = $(this).parents('tr');
            var count = p.parent().find('tr').length;
            var index = p.parent().find('tr').index(p);
            if (index == (count - 1))
                return false;
            else {
                var oriRowNumber = parseInt(p.attr('rowNumber'));
                var newRowNumber = p.next().attr('rowNumber');

                changeSequence(oriRowNumber, newRowNumber, function () {
                    p.attr('rowNumber', newRowNumber);
                    p.next().attr('rowNumber', oriRowNumber);
                    p.next().after(p);
                    reDrawArrow(thisObj);
                });
            }
        });



    });


    function reDrawArrow(obj) {
        $(obj).parents('tbody').find('.glyphicon').removeClass('disabled');
        $(obj).parents('tbody').find('tr').first().find('.glyphicon-circle-arrow-up').addClass('disabled');
        $(obj).parents('tbody').find('tr').last().find('.glyphicon-circle-arrow-down').addClass('disabled');
    }
</script>
